$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/rewardsconfig/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true },
			{ label: '运营中心ID', name: 'operationId', index: 'operation_id', width: 50,hidden:true },
			{ label: '运营中心', name: 'operationName', index: 'operation_name', width: 50 },
			{ label: '类型', name: 'typeDesc', index: 'type_desc', width: 80 },
			{ label: '类型ID', name: 'type', index: 'type', width: 80 ,hidden:true},
			{ label: '最低业绩要求', name: 'minPerformance', index: 'min_performance', width: 80 },
			{ label: '代理等级', name: 'levelName', index: 'level_name', width: 80 },
            { label: '代理等级ID', name: 'level', index: 'level', width: 80 ,hidden:true},
			{ label: '提成比例', name: 'ratio', index: 'ratio', width: 80 },
			{ label: '状态', name: 'status', index: 'status', width: 80,formatter: function (value, options, row) {
                    if(value==1){
                        return "<span>正常</span>";
                    }else{
                        return "<span>禁用</span>";
                    }
                }},
            { label: '创建时间', name: 'createTime', index: 'create_time', width: 80 },
            { label: '修改时间', name: 'updateTime', index: 'update_time', width: 80 }
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});
var rewardTypeCode ;
var userLevelCode ;
var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		rewardsConfig: {},
		agentList: null
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.rewardsConfig = {
                status:1
            };
            // vm.getAgentLevel();
            getAllRewardsEnumBy(null);
            getAllUserLevelEnumBy(null);
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            var rowData=jQuery("#jqGrid").jqGrid("getRowData",id);
            userLevelCode = rowData.level;
            rewardTypeCode = rowData.type;
            getAllRewardsEnumBy(rewardTypeCode);
            getAllUserLevelEnumBy(userLevelCode);
            // vm.getAgentLevel();
            vm.getInfo(id);
		},
		saveOrUpdate: function (event) {
		    $('#btnSaveOrUpdate').button('loading').delay(1000).queue(function() {
                var url = vm.rewardsConfig.id == null ? "sys/rewardsconfig/save" : "sys/rewardsconfig/update";
                var levelStr = $("#userLevel option:selected").text();
                var levelName = levelStr.split("-")[0];
                var userLevel = levelStr.split("-")[1];
                vm.rewardsConfig.level = userLevel;
                vm.rewardsConfig.levelName = levelName;
                var rewardStr = $("#rewardType option:selected").text();
                var rewardName = rewardStr.split("-")[0];
                var rewardType = rewardStr.split("-")[1];
                vm.rewardsConfig.type = rewardType;
                vm.rewardsConfig.typeDesc = rewardName;
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.rewardsConfig),
                    success: function(r){
                        if(r.code === 0){
                             layer.msg("操作成功", {icon: 1});
                             vm.reload();
                             $('#btnSaveOrUpdate').button('reset');
                             $('#btnSaveOrUpdate').dequeue();
                        }else{
                            layer.alert(r.msg);
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        }
                    }
                });
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			var lock = false;
            layer.confirm('确定要删除选中的记录？', {
                btn: ['确定','取消'] //按钮
            }, function(){
               if(!lock) {
                    lock = true;
		            $.ajax({
                        type: "POST",
                        url: baseURL + "sys/rewardsconfig/delete",
                        contentType: "application/json",
                        data: JSON.stringify(ids),
                        success: function(r){
                            if(r.code == 0){
                                layer.msg("操作成功", {icon: 1});
                                $("#jqGrid").trigger("reloadGrid");
                            }else{
                                layer.alert(r.msg);
                            }
                        }
				    });
			    }
             }, function(){
             });
		},
		getInfo: function(id){
			$.get(baseURL + "sys/rewardsconfig/info/"+id, function(r){
                vm.rewardsConfig = r.rewardsConfig;
                rewardTypeCode = r.rewardsConfig.type;
            });
		},
        // getAgentLevel: function(){
        //     $.ajax({
        //         type: "GET",
        //         url: baseURL + "sys/userLevel/agentList",
        //         success: function(r){
        //             if(r.code === 0){
        //                 vm.agentList = r.data;
        //             }else{
        //                 layer.alert(r.msg);
        //             }
        //         }
        //     });
        // },
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:1
            }).trigger("reloadGrid");
		}
	}
});

function getAllRewardsEnumBy(rewardTypeCode){
    $.ajax({
        type: "POST",
        url: baseURL + "sys/rewardsconfig/getAllEnumBy",
        contentType: "application/json",
        success: function(r){
            if(r.code == 0){
                $("#rewardType").html(""); //绑定模号下拉菜单
                for (var i = 0; i < r.data.length; i++) {
                    if(null != rewardTypeCode && r.data[i].code == rewardTypeCode){
                        $("#rewardType").append($("<option value="+r.data[i].code+" selected>" +r.data[i].desc+"-"+r.data[i].code+ "</option>"));
                    }else{
                        $("#rewardType").append($("<option value="+r.data[i].code+">" +r.data[i].desc+"-"+r.data[i].code+ "</option>"));
                    }
                }
            }else{
                alert(r.msg);
            }
        }
    });
}

function getAllUserLevelEnumBy(userLevelCode){
    $.ajax({
        type: "POST",
        url: baseURL + "sys/userLevel/getAllEnumBy",
        contentType: "application/json",
        success: function(r){
            if(r.code == 0){
                $("#userLevel").html(""); //绑定模号下拉菜单
                for (var i = 0; i < r.data.length; i++) {
                    if(null != userLevelCode && r.data[i].code == userLevelCode){
                        $("#userLevel").append($("<option value="+r.data[i].code+" selected>" +r.data[i].desc+"-"+r.data[i].code+ "</option>"));
                    }else{
                        $("#userLevel").append($("<option value="+r.data[i].code+">" +r.data[i].desc+"-"+r.data[i].code+ "</option>"));
                    }
                }
            }else{
                alert(r.msg);
            }
        }
    });
}